package com.yc.structuralType.proxy;

import javax.swing.*;
import java.awt.*;
import java.util.HashMap;
import java.util.Map;

public class RealSubject extends JFrame implements Subject {

    public RealSubject() {
        super("代理模式测试");
        this.setLayout(new GridLayout(1, 1));
        JLabel jl = new JLabel(new ImageIcon("src/WuyuanSpecialty.jpg"));
        this.add(jl);
        this.pack();
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    // E:\IdeaProjects\DesignModule\src\com\proxy\WuyuanSpecialty.jpg

    @Override
    public void display() {
        this.setVisible(true);
    }



    //
    public static int[] twoSum(int[] nums, int target) {
        Map<Integer, Integer> map = new HashMap<>();
        for (int i = 0; i < nums.length; i++) {
            int complement = target - nums[i];
            if (map.containsKey(complement)) {
                return new int[] { map.get(complement), i };
            }
            map.put(nums[i], i);
        }
        throw new IllegalArgumentException("No two sum solution");
    }

    public static void main(String[] args) {
        int[] a = twoSum(new int[]{2,5,5,7,8},10);
        for (int i=0; i<a.length; i++) {
            System.out.println(a[i]);
        }
    }

    // 两数乘积，按字符串输入
    public String multiply(String num1, String num2) {
        if (num1.equals("0") || num2.equals("0")) {
            return "0";
        }
        int[] res = new int[num1.length() + num2.length()];
        for (int i = num1.length() - 1; i >= 0; i--) {
            int n1 = num1.charAt(i) - '0';
            for (int j = num2.length() - 1; j >= 0; j--) {
                int n2 = num2.charAt(j) - '0';
                int sum = (res[i + j + 1] + n1 * n2);
                res[i + j + 1] = sum % 10;
                res[i + j] += sum / 10;
            }
        }

        StringBuilder result = new StringBuilder();
        for (int i = 0; i < res.length; i++) {
            if (i == 0 && res[i] == 0) continue;
            result.append(res[i]);
        }
        return result.toString();
    }
}
